package org.jeecg.modules.basesource.handler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.qyxx.service.ICjQyxxStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 初始化每月要跑的数据
 * @author chensherun on 2022/3/9.
 * @version 1.0
 */
@Slf4j
@Component
public class CjQyxxStatusHandler {

    @Autowired
    private ICjQyxxStatusService cjQyxxStatusService;

    /**
     * 每月采集状态初始化
     * @param
     * @return
     */
    @XxlJob(value = "cjQyxxStatusInitJob")
    public ReturnT<String> cjQyxxStatusInitJob(String param) {
        Calendar cal = Calendar.getInstance();
        //上月
        cal.add(Calendar.MONTH, -1);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        cal.set(year, month, 1, 00, 00, 00);
        //上月第一天
        Date first = cal.getTime();
        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
        //上月最后一天
        Date last = cal.getTime();
        cjQyxxStatusService.initBatchMonthCjStatus(first, last);
        return ReturnT.SUCCESS;
    }

    /**
     * 将新增企业加入到同步初始化
     * @param param
     * @return
     */
    @XxlJob(value = "initFirstTimeCjStatus")
    public ReturnT<String> initFirstTimeCjStatus(String param) {
        //获取当月新增但cjQyxxStatus里面没有数据的企业列表
        List<DjOrg> orgList = cjQyxxStatusService.getNeedFirstInitOrgList();
        if (orgList == null || orgList.size() == 0) {
            return ReturnT.SUCCESS;
        }
        for (DjOrg djOrg : orgList) {
            cjQyxxStatusService.initFirstTimeCjStatus(djOrg, false);
        }
        return ReturnT.SUCCESS;
    }
}
